<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="产品代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productCode">
              <j-popup
                v-model="model.productCode"
                field="productCode"
                org-fields="product_code,product_name,product_id,unit,sap,price,tax_price,tax,totalnum"
                dest-fields="productCode,productName,productId,unit,sap,price,taxPrice,tax,totalnum"
                code="custom_material_select"
                :param="param1"
                :multi="true"
                @input="popupCallback"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产品名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productName">
              <a-input v-model="model.productName" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="false">
            <a-form-model-item label="产品Id">
              <a-input v-model="model.productId"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sap">
              <a-input v-model="model.sap" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <a-input v-model="model.unit" disabled></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input-number v-model="model.price" disabled style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="含税单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taxPrice">
              <a-input-number v-model="model.taxPrice" disabled style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="税率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tax">
              <a-input-number v-model="model.tax" disabled style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="number">
              <a-input-number v-model="model.number" placeholder="请输入数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="价税合计" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totoaPrice">
              <a-input-number v-model="model.totoaPrice" disabled style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="仓库" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="warehouse">
              <j-dict-select-tag
                type="list"
                v-model="model.warehouse"
                dictCode="hf_warehouse,warehouse_name,id"
                placeholder="请选择仓库"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="库存" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="totalnum" placeholder="数量不能超过库存" disabled></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'HfSaleoutDetailModal',
  components: {},
  props: {
    mainId: {
      type: String,
      required: false,
      default: ''
    },
    param: {}
  },
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      totalnum: '0',
      isRed: '2',
      param1: { name: '' },
      model: {
        totoaPrice: '0'
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        materialName: [{ required: true, message: '请输入物料!' }],
        warehouse: [{ required: true, message: '请输入发料仓库!' }],
        number: [{ required: true, message: '请输入数量!' }]
      },
      url: {
        add: '/sale/hfSaleOut/addHfSaleoutDetail',
        edit: '/sale/hfSaleOut/editHfSaleoutDetail'
      }
    }
  },
  watch: {
    'model.number': {
      deep: true,
      immediate: true,
      handler(newValue) {
        if (newValue != undefined) {
          this.model.totoaPrice = Math.round(this.model.taxPrice * newValue * 100) / 100
        } else {
          this.model.totoaPrice = 0
        }
      }
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.param = this.param1
    console.log('param1', this.param1)
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      if (that.isRed == '2' && that.totalnum < that.model.number) {
        this.$message.error('出库数量不能超过库存!')
        return
      }
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          this.model['orderId'] = this.mainId
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(value, row) {
      this.totalnum = row.totalnum
      this.model = Object.assign(this.model, row)
    }
  }
}
</script>
